//package com.yunhe.device.service.device.impl;
//
//import com.yunhe.device.domain.device.DeviceTree;
//import com.yunhe.device.repository.device.DeviceTreeRepository;
//import com.yunhe.device.repository.device.impl.DeviceTreeDao;
//import com.yunhe.device.service.device.DeviceTreeService;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//
//import java.util.*;
//
//@Service
//public class DeviceTreeServiceImpl implements DeviceTreeService {
//
//    @Autowired
//    private DeviceTreeRepository deviceTreeRepository;
//    @Autowired
//    private DeviceTreeDao deviceTreeDao;
//
//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public DeviceTree create(Long stationCode, Long devCode, Long parentId) {
//        DeviceTree deviceTree;
//        deviceTreeRepository.deleteRelationship(devCode);
//        if (parentId != null) {
//            deviceTree = deviceTreeRepository.createDeviceTreeNode(stationCode, devCode);
//            Long treeId = deviceTreeRepository.findDeviceId(parentId);
//            Optional<DeviceTree> parentOpt = deviceTreeRepository.findById(treeId);
//            if (parentOpt.isPresent()) {
//                DeviceTree parent = parentOpt.get();
//                List<DeviceTree> children = parent.getChildren();
//                if (children == null) {
//                    children = new ArrayList<>();
//                }
//                children.add(deviceTree);
//                parent.setChildren(children);
//                deviceTreeRepository.save(parent);
//            }
//        } else {
//            deviceTree = deviceTreeRepository.createDeviceTreeNode(stationCode, devCode);
//        }
//        Long id = deviceTree.getId();
//        Map<String, Map<String, Object>> map = deviceTreeDao.getDeviceTree(Arrays.asList(id));
//        Map<String, Object> nodeMap = map.get(String.valueOf(id));
//        deviceTree.setId(((Number) nodeMap.get("id")).longValue());
//        deviceTree.setCode(((Number) nodeMap.get("code")).longValue());
//        deviceTree.setType((String) nodeMap.get("type"));
//        deviceTree.setTitle((String) nodeMap.get("title"));
//        return deviceTree;
//    }
//
//    @Override
//    public void delete(Long id) {
//        deviceTreeRepository.deleteById(id);
//    }
//
//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public void update(Long id, Long deviceId, Long parentId) {
//        deviceTreeRepository.updateDevice(id, deviceId);
//        if (parentId != null) {
//            deviceTreeRepository.updateParent(id, parentId);
//        }
//    }
//
//    @Override
//    public Iterable<DeviceTree> search(Long stationCode) {
//        List<DeviceTree> deviceTrees = (List<DeviceTree>)deviceTreeRepository.findByStationCode(stationCode);
//        List<Long> idList = new ArrayList<>();
//        for (DeviceTree deviceTree : deviceTrees) {
//            idList.add(deviceTree.getId());
//        }
//        process(deviceTrees, idList);
//        removeRepeatChild(deviceTrees);
//        sortDeviceTree(deviceTrees);
////        //添加根节点
////        if (deviceTrees.size() > 0) {
////            DeviceTree deviceTree = deviceTrees.get(0);
////            for (DeviceTree tree : deviceTrees) {
////                DeviceTree root = deviceTreeRepository.findRootById(deviceTree.getId());
////            }
////        }
//        return deviceTrees;
//    }
//
//    public void sortDeviceTree(List<DeviceTree> list) {
//        sort(list);
//        if (list != null) {
//            for (DeviceTree deviceTree : list) {
//                List<DeviceTree> children = deviceTree.getChildren();
//                sort(children);
//                deviceTree.setChildren(children);
//                sortDeviceTree(children);
//            }
//        }
//    }
//
//    private void sort(List<DeviceTree> list) {
//        if (list != null) {
//            list.sort((o1, o2) -> {
//                if (o1.getCode() == null || o2.getCode() == null) {
//                    return 0;
//                }
//                if (o1.getCode() > o2.getCode()) {
//                    return 1;
//                } else if (o1.getCode() < o2.getCode()) {
//                    return -1;
//                } else {
//                    return 0;
//                }
//            });
//        }
//    }
//
//    private void removeRepeatChild(List<DeviceTree> list) {
//        if (list != null && list.size() > 0) {
//            //保存重复的ID
//            Map<Long, Object> repeatIdMap = new HashMap<>();
//            //第一次循环，获得所有孩子节点ID
//            for (DeviceTree deviceTree : list) {
//                List<DeviceTree> children =  deviceTree.getChildren();
//                if (children != null && children.size() > 0) {
//                    for (DeviceTree child: children) {
//                        repeatIdMap.put(child.getId(), null);
//                    }
//                }
//            }
//            //根据是否是孩子节点移除元素
//            for (int i = list.size() - 1; i >= 0; i--) {
//                DeviceTree deviceTree = list.get(i);
//                if (repeatIdMap.containsKey(deviceTree.getId())) {
//                    list.remove(i);
//                }
//            }
//        }
//    }
//
//    private void process(List<DeviceTree> deviceTrees, List<Long> idList) {
//        Map<String, Map<String, Object>> map = deviceTreeDao.getDeviceTree(idList);
//        for (DeviceTree deviceTree : deviceTrees) {
//            Map<String, Object> valueMap = map.get(deviceTree.getId().toString());
//            if (valueMap != null && valueMap.size() == 4) {
//                deviceTree.setId(((Number) valueMap.get("id")).longValue());
//                deviceTree.setCode(((Number) valueMap.get("code")).longValue());
//                deviceTree.setTitle((String) valueMap.get("title"));
//                deviceTree.setType((String) valueMap.get("type"));
//                List<DeviceTree> children = deviceTree.getChildren();
//                if (children != null && children.size() > 0) {
//                    List<Long> list = new ArrayList<>();
//                    for (DeviceTree tree : children) {
//                        list.add(tree.getId());
//                    }
//                    process(children, list);
//                }
//            }
//        }
//    }
//
////    @Override
////    public List<DeviceTree> search(Long firmCode, Optional<Long> stationCode) {
////        List<DeviceTree> list;
////        if (stationCode.isPresent()) {
////            list = deviceTreeRepository.getDeviceTree(stationCode.get());
////        } else {
////            list = deviceTreeRepository.getDeviceTree(firmCode);
////        }
////        if (list != null && list.size() > 0) {
////            Collections.sort(list);
////            Integer level = list.get(list.size() - 1).getLevel();
////            for (DeviceTree tree : list) {
////                List<DeviceTree> deviceTree = deviceTreeDao.getDeviceTree(tree.getId());
////                if (tree.getLevel().intValue() == level) {
////                    List<DeviceTree> children = tree.getChildren();
////                    if (children == null) {
////                        children = new ArrayList<>();
////                        children.add(objectToDeviceTree(tree.getChild()));
////                        tree.setChildren(children);
////                    }
////                }
////            }
////            process(list);
////            List<DeviceTree> children;
////            List<DeviceTree> removeList = new ArrayList<>();
////            for (DeviceTree device : list) {
////                for (DeviceTree deviceTree : list) {
////                    if (device.getChild().equals(deviceTree.getObject())) {
////                        children = device.getChildren();
////                        if (children == null) {
////                            children = new ArrayList<>();
////                        }
////                        if (!contains(children, deviceTree)) {
////                            children.add(deviceTree);
////                        }
////                        device.setChildren(children);
////                        removeList.add(deviceTree);
////                    }
////                }
////            }
////            list.removeAll(removeList);
////            Collections.sort(list);
////            return list;
////        }
////        return null;
////    }
//
////    private List<DeviceTree> process(List<DeviceTree> list, DeviceTree deviceTree) {
////        Integer level = deviceTree.getLevel();
////        List<DeviceTree> rlist = new ArrayList<>();
////        for (DeviceTree tree : list) {
////            if (deviceTree.getChild() != null) {
////                if (tree.getLevel() == level + 1 && objectToDeviceTree(deviceTree.getChild()).getId().longValue() == tree.getId()) {
//////                if (tree.getLevel() == level + 1 && contains(deviceTree.getChildren(), tree)) {
////                    List<DeviceTree> children = deviceTree.getChildren();
////                    if (children == null) {
////                        children = new ArrayList<>();
////                    }
////                    if (!contains(children, tree)) {
////                        children.add(tree);
////                        deviceTree.setChildren(children);
////                    }
////                    rlist.add(tree);
////                }
////            }
////        }
////        return rlist;
////    }
////
////    private void test(List<DeviceTree> list) {
////        Map<Long, List<DeviceTree>> map = new HashMap<>();
////        for (DeviceTree deviceTree : list) {
////            if (map.get(deviceTree.getId()) == null) {
////                List<DeviceTree> deviceTrees = new ArrayList<>();
////                deviceTrees.add(deviceTree);
////                map.put(deviceTree.getId(), deviceTrees);
////            } else {
////                List<DeviceTree> deviceTrees = map.get(deviceTree.getId());
////                deviceTrees.add(deviceTree);
////                map.put(deviceTree.getId(), deviceTrees);
////            }
////        }
////        for (Map.Entry<Long, List<DeviceTree>> entry : map.entrySet()) {
////            List<DeviceTree> value = entry.getValue();
////            if (value != null && value.size() > 1) {
////                DeviceTree newTree = new DeviceTree();
////                DeviceTree deviceTree = value.get(0);
////                newTree.setId(deviceTree.getId());
////                newTree.setCode(deviceTree.getCode());
////                newTree.setTitle(deviceTree.getTitle());
////                newTree.setType(deviceTree.getType());
////                newTree.setLevel(deviceTree.getLevel());
////                List<DeviceTree> children = new ArrayList<>();
////                for (DeviceTree tree : value) {
////                    children.add(objectToDeviceTree(tree.getChild()));
////                }
////                newTree.setChildren(children);
////                list.removeAll(value);
////                list.add(newTree);
////            }
////        }
////    }
////
////    /**
////     *
////     * @param list
////     */
////    private void process(List<DeviceTree> list) {
////        List<DeviceTree> removeList = new ArrayList<>();
////        for (DeviceTree deviceTree : list) {
////            for (DeviceTree tree : list) {
////                if (deviceTree.getId().intValue() == tree.getId() && !deviceTree.getChild().equals(tree.getChild())) {
////                    List<DeviceTree> children = deviceTree.getChildren();
////                    Object child = tree.getChild();
////                    if (children == null) {
////                        children = new ArrayList<>();
////                    }
////                    children.add(objectToDeviceTree(child));
////                    deviceTree.setChildren(children);
////                    if (!contains(removeList, tree)) {
////                        removeList.add(tree);
////                    }
////                }
////            }
////        }
////        list.removeAll(removeList);
////    }
////
////    private Boolean contains(List<DeviceTree> list, DeviceTree deviceTree) {
////        for (DeviceTree tree : list) {
////            if (tree.getId().longValue() == deviceTree.getId()) {
////                return true;
////            }
////        }
////        return false;
////    }
////
////    private DeviceTree objectToDeviceTree(Object object) {
////        DeviceTree deviceTree = new DeviceTree();
////        JSONObject jsonObject = new JSONObject(object);
////        Long id = jsonObject.getLong("id");
////        Long code = jsonObject.getLong("code");
////        String title = jsonObject.getString("title");
////        deviceTree.setId(id);
////        deviceTree.setTitle(title);
////        deviceTree.setCode(code);
////        String type = deviceTreeRepository.getTypeById(id);
////        deviceTree.setType(type);
////        deviceTree.setObject(object);
////        return deviceTree;
////    }
//}
